package com.itheima.hashset3;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Teacher implements Comparable<Teacher> {
    //解决方案1：对象类实现一个Comparable<T>比较接口，重写compareTo方法，指定排序规则

    private String name;
    private int age;
    private double salary;

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}' + "\n";
    }

    //t2.compareTo(t1)
    //t2==this，t1==o
    //规定：左边大于右边返回正整数，左边小于右边返回负整数，相等返回0
    @Override
    public int compareTo(Teacher o) {
        /*if (this.age > o.age) return 1;
        if (this.age < o.age) return -1;
        //return 0;
        return 1;   //保留相同年龄的老师*/

        return this.age - o.age;        //升序
        //return o.age - this.age;      //降序

    }
}
